package advanced;

import java.lang.reflect.*;
import java.util.List;
import java.util.Map;

/**
 * 反射操作泛型(Generic)
 * 泛型在编译完成之后就会被编译器擦除掉
 * <p>
 * 为了通过反射操作这些类型以迎合实际开发的需要,Java就增加了ParameterizedType, GenericArrayType, TypeVariable和WildcardType
 * 几种类型来代表不能被鬼一道class类中国的类型但是有何原始类型齐名的类型
 * <p>
 * 最常用的是: ParameterizedType
 */

public class L_Reflection_T {
    public static void main(String[] args) throws ClassNotFoundException, IllegalAccessException, InstantiationException, NoSuchMethodException, InvocationTargetException, NoSuchFieldException {
        Method m1 = L_Reflection_T.class.getMethod("test01", Map.class, List.class);
        Type[] mt = m1.getGenericParameterTypes();
        for (Type type : mt) {
            System.out.println("#" + type);
            if (type instanceof ParameterizedType) {
                Type[] actualTypeArguments = ((ParameterizedType) type).getActualTypeArguments();
                for (Type genericType : actualTypeArguments) {
                    System.out.println("类型泛型:" + genericType);
                }
            }
        }
        System.out.println("-------------------");
        Method m2 = L_Reflection_T.class.getMethod("test02", Map.class, List.class);
        Type returnType = m2.getGenericReturnType();
        System.out.println(" 返回值类型" + returnType);
        if (returnType instanceof ParameterizedType) {
            Type[] actualTypeArguments = ((ParameterizedType) returnType).getActualTypeArguments();
            for (Type genericType : actualTypeArguments) {
                System.out.println("类型泛型:" + genericType);
            }
        }
    }

    public void test01(Map<String, User> map, List<User> list) {
        System.out.println("反射处理泛型");

    }

    public Map<Integer, User> test02(Map<String, User> map, List<User> list) {
        System.out.println("反射处理泛型2");
        return null;

    }

}

class User {
}

